﻿using Updater.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.IO;
using Updater.Common.ServiceReference;

namespace Updater.Service.UnitTest
{        
    /// <summary>
    ///This is a test class for DownloaderTest and is intended
    ///to contain all DownloaderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DownloaderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes        
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        #region Methods run before/after tests
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            //Downloader.CopySettings();
        }

        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            //Downloader.RestoreSettings();
        }

        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            //Downloader.SetTestSettings();
        }

        #endregion

        /// <summary>
        ///A test for DownloadFile
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Updater.Common.dll")]
        public void DownloadFileTest()
        {
            Downloader_Accessor target = new Downloader_Accessor(); // Use this class to gain access to private methods.            

            ServiceClient serviceClient = new ServiceClient("WSHttpBinding_IService");
            serviceClient.Open();

            string filename = "This file does not exist";
            bool expected = false;
            bool actual;
            actual = target.DownloadFile(filename, serviceClient);
            Assert.AreEqual(expected, actual, "Successful download of non-existing file.");

            filename = "TestTransfer.1.2.7.1.msi"; // This should be a file which is available for download!
            expected = true;
            actual = target.DownloadFile(filename, serviceClient);
            Assert.AreEqual(expected, actual, "File was not downloaded properly.");

            serviceClient.Close();
        }

        /// <summary>
        ///A test for DoDownloads
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Updater.Common.dll")]
        public void DoDownloadsTest()
        {
            Downloader target = new Downloader();
            string testFilename = target.DownloadPath + "TestTransfer.1.2.7.1.msi";
            string testBetaFilename = target.DownloadPath + "TestTransfer_BETA.1.2.7.1.msi";
            string testNoGoFilename = target.DownloadPath + "TestTransfer.9.0.0.0.msi";


            foreach (string eraseFile in Directory.GetFiles(target.DownloadPath, "TestTransfer*.msi"))
            {
                File.Delete(eraseFile);
            }

            target.DoDownloads();

            Downloader_Accessor testTarget = new Downloader_Accessor();
            Assert.IsTrue(File.Exists(testFilename), testFilename + " was not downloaded.");
            Assert.IsTrue(File.Exists(testBetaFilename), testBetaFilename + " was not downloaded.");
            Assert.IsFalse(File.Exists(testNoGoFilename), testNoGoFilename + " was downloaded.");            
            Assert.AreEqual("TestTransfer.1.2.7.1", testTarget.CreateAssemblyVersionString("TestTransfer"), "Wrong assembly version created.");
            Assert.AreEqual("TestDummy.0.0.0.0", testTarget.CreateAssemblyVersionString("TestDummy"), "Non-zero assembly version created.");            
        }

        /// <summary>
        ///A test for DoUpdates
        ///</summary>
        [TestMethod()]
        public void DoUpdatesTest()
        {
            Downloader_Accessor target = new Downloader_Accessor(); // TODO: Initialize to an appropriate value
            foreach (string eraseFile1 in Directory.GetFiles(target.DownloadPath, "TestTransfer*.msi"))            
                File.Delete(eraseFile1);            
            foreach( string eraseFile2 in Directory.GetFiles(target.siteInfos[0].DirectoryToUpdate))
                File.Delete(eraseFile2);
            foreach( string eraseFile3 in Directory.GetFiles(target.siteInfos[1].DirectoryToUpdate))
                File.Delete(eraseFile3);

            string testFilename = target.DownloadPath + "TestTransfer.1.2.7.1.msi";            
            using (StreamWriter writer1 = File.CreateText(testFilename))
                {
                    writer1.Write("Test");
                }
            
            string targetFilename = target.siteInfos[0].DirectoryToUpdate + "TestTransfer.1.2.7.1.msi";           

            string testOldFilename = target.DownloadPath + "TestTransfer.0.0.0.1.msi";
                using (StreamWriter writer3 = File.CreateText(testOldFilename))
                {
                    writer3.Write("Test");
                }

            string targetOldFilename = target.siteInfos[0].DirectoryToUpdate + "TestTransfer.0.0.0.1.msi";          

            string testNewFilename = target.DownloadPath + "TestTransfer.3.0.0.1.msi";
                using (StreamWriter writer4 = File.CreateText(testNewFilename))
                {
                    writer4.Write("Test");
                }

            string targetNewFilename = target.siteInfos[0].DirectoryToUpdate + "TestTransfer.3.0.0.1.msi";
            
            string testBetaFilename = target.DownloadPath + "TestTransfer_BETA.1.2.7.1.msi";
                using (StreamWriter writer2 = File.CreateText(testBetaFilename))
                {
                    writer2.Write("Test");
                }

            string targetBetaFilename = target.siteInfos[1].DirectoryToUpdate + "TestTransfer_BETA.1.2.7.1.msi";
            
            target.DoUpdates();
            Assert.IsTrue(File.Exists(targetFilename), "File TestTransfer.1.2.7.1.msi was not updated.");
            Assert.IsTrue(File.Exists(targetNewFilename), "File TestTransfer.3.0.0.1.msi was not updated.");
            Assert.IsTrue(File.Exists(targetBetaFilename), "File TestTransfer_BETA.1.2.7.1.msi was not updated.");
            Assert.IsFalse(File.Exists(targetOldFilename), "File TestTransfer.0.0.0.1.msi was incorrectly updated.");

            Downloader_Accessor testTarget = new Downloader_Accessor();
            Assert.IsTrue(testTarget.siteInfos[0].LastUpdate.Date.Equals(System.DateTime.Now.Date), "Date was not correctly saved.");
        }
    }
}
